import {
  findImportTagsList,
  findByPMapper,
  addContentTags
} from '@/api/tags'
export default {
  methods: {
    idGetTagList(taglist) {
      let newTages = []
      //
      if (!taglist) return newTages;
      if(!taglist.filter(item=>Boolean(item.tagId)===true).length) return taglist;
      if (Array.isArray(taglist)) {
        taglist.forEach(tag => {
          let b = {}
          b[tag['tagName']] = []
          console.log('d919174106b3951f tag sdsd',tag)
          for (let tagsId in tag.tags) {
            let tagsName = tag.tags[tagsId]
            b[tag['tagName']].push({
              tagsId,
              tagsName
            })
          }
          newTages.push(b)
        })
      }
      return newTages
    },
    delFile(list){
    this.info.fileList.map(files=>{
      return list.id === files.id?list:files
    })
    this.save(list)
    },
    // 删除 tags 方法
    delCanback(item) {

      this.save(item)
    },
    editTagList(item) { // j import 下标
      this.dialogVisible = true
      this.checkedVal = item
      const kName = item.k
      // 所选的重点内容的tagsId
      if (this.importTagsList.length) {
        this.getFindByPMapper(this.tagsNamefindTags(kName))
      } else {
        this.getFindImportTags(kName)
      }
    },
    tagsNamefindTags(kName) {
      let thisId = this.importTagsList[0].tagsId
      if (kName) {
        let isNameObj = this.importTagsList.filter(item => item.tagsName === kName)
        if (Array.isArray(isNameObj)) thisId = isNameObj[0].tagsId
      }
      return thisId
    },
    // 重难点
    getFindImportTags(kName) {
      let data = {
        ...this.getClassfiyArr,
        tagsGroupEnum: 'IMPORTANT'
      }
      findImportTagsList(data).then(res => {
        this.importTagsList = res.data

        if (this.checkedVal && this.importTagsList.length) {
          this.checkKeynote = this.importTagsList.filter(list => {
            return list.tagsName === this.checkedVal.k
          })
        }
        if(kName){
          let thisId = this.tagsNamefindTags(kName)
          this.getFindByPMapper(thisId)
        }

      })
    },
    // 能力
    getFindByPMapper(id) {
      let data = {
        pMapperTagsId: id
      }
      findByPMapper(data).then(res => {
        this.ByPMapperList = res.data
        let that = this
        if (this.checkedVal && this.ByPMapperList.length) {
          if (Array.isArray(that.checkedVal.tagkey)) {
            that.checkedVal.tagkey.forEach(item => {
              that.checkAbilityArr = that.ByPMapperList.filter(list => {
                return list.tagsId === item.tagsId
              })
            })
          }
        }
        console.log('ByPMapperList:', this.ByPMapperList)
      })
    },
    changeImportCheckBox(item) {
      this.getFindByPMapper(item.tagsId)
    },
    dialogCallback(obj) {
      // obj.val 编辑或者添加选择的值
      console.log('dialogCallback:', obj)
      this.dialogVisible = obj.dialogVisible
      const tagGroudArr = obj.val // {keynote:obj.keynote,abilityArr:obj.abilityArr}
      let falog = false
      this.tagsList.map((item, j, k) => {
        for (let kn in item) {
          if (JSON.stringify(tagGroudArr).indexOf(kn) > -1) {
            falog = {
              j,
              k
            }
          }
        }
      })
      // 编辑,添加
      if (!obj.edit && !falog) {
        this.tagsList.push(tagGroudArr)
      } else {
        let {
          j,
          k
        } = obj.edit ? obj.edit : falog

        console.log('j', j, k, obj.val)
        this.$set(this.tagsList[j], k, obj.val[k])
      }
    },
    getTagListSave(indtagList){

      let that = this,tagList = [];
      var improvementTagList = [],keyPointTagList = []
      //整合所要保存的标签 分类
         if(Array.isArray(indtagList)){
          indtagList.forEach(taLis=>{
            tagList.push(taLis)
          })
         }else if(typeof(indtagList)==="object"){
           for(let taLis in indtagList){
             tagList.push(taLis)
           }
         }

      //
        for (let tag in tagList) {
          let keyPoint = tagList[tag]
          for (let keyP in keyPoint) {
            let impor = this.importTagsList.filter(item => item.tagsName === keyP)
            //impor = impor.length?impor:keyPoint

            impor.map(imp => {

              improvementTagList.push({
                id: imp.tagsId
              })
            })
              let tagsItem = keyPoint[keyP]

              if(tagsItem){
              tagsItem.map((sitem) => {
                keyPointTagList.push({
                  id: sitem.tagsId
                })
              })
            }
          }
        }
        return {improvementTagList,keyPointTagList}
      //   console.log('this.tagList', that.improvementTagList,that.keyPointTagList)
    }
  }
}
